package gw.com.android.ui.trade;

import android.os.Bundle;
import android.view.View;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.AppContances;
import gw.com.android.ui.trade.Fragment.BaseOrderFragment;
import gw.com.android.ui.trade.Fragment.MarketOrderModifyFragment;
import gw.com.android.ui.trade.Fragment.OrderFailFragment;
import gw.com.android.ui.trade.Fragment.OrderSucFragment;
import www.com.library.app.FragmentsManagerUtil;
import www.com.library.model.DataItemDetail;
import www.com.library.view.BtnClickListener;

/**
 * 止损止盈设置界面
 * @author reeta
 *
 */
public class OrderModifyActivity extends TradeOrderActivity{

	private MarketOrderModifyFragment mModifyOrderFragment;

	@Override
	protected void setAcitivityParam() {
		super.setAcitivityParam();
		if(getIntent() != null){
			Bundle bundle = getIntent().getBundleExtra("params");
			mFromType = bundle.getInt("fromType", AppContances.TRADE_TYPE_FORM_QUOTE);
			mUiCode = bundle.getInt("uiCode", 0);
		}
	}

	@Override
	protected void initLayoutView() {
        super.initLayoutView();
		/*if (mFromType == AppContances.TRADE_TYPE_FORM_QUOTE) {
			mTitleBar.setLeftText(AppMain.getAppString(R.string.main_menu_item_quote));
		} else if (mFromType == AppContances.TRADE_TYPE_FORM_CHART) {
			mTitleBar.setLeftText(AppMain.getAppString(R.string.main_menu_item_chart));
		} else if (mFromType == AppContances.TRADE_TYPE_FORM_TRADE) {
			mTitleBar.setLeftText(AppMain.getAppString(R.string.order_type_position));
		}*/
		mTitleBar.setAppTitle(R.string.order_type_modify_market);
	}

	@Override
	protected void initViewData() {

	}

	/**
	 * 返回bundle对象
	 */
	private Bundle getBundle(){
		Bundle bundle = getIntent().getBundleExtra("params");
    	return bundle;
	}
	
	/**
	 * 展示设置止损止盈界面
	 */
	public void showOrderFragment(){
		super.showOrderFragment();
		mTypeTag = AppContances.TAB_ID_MODIFY_ORDER;
		mCurrentTag = AppContances.TAB_ID_MODIFY_ORDER;
		mModifyOrderFragment = (MarketOrderModifyFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
        if(null == mModifyOrderFragment){
        	mModifyOrderFragment = new MarketOrderModifyFragment();
        	mModifyOrderFragment.setArguments(getBundle());
        	mModifyOrderFragment.setOrderStatusListener(new BaseOrderFragment.onOrderStatusListener() {
				
				@Override
				public void onSuc(DataItemDetail mDetail) {
                    mDetailItem.clear().append(mDetail);
                    mHandler.sendEmptyMessage(AppContances.HANDLER_RESULT_SUC);
				}
				
				@Override
				public void onFail(DataItemDetail mDetail) {
					mDetailItem.clear().append(mDetail);
					mHandler.sendEmptyMessage(AppContances.HANDLER_SERVER_FAIL);
				}

				@Override
				public void onFail() {
					hideLoading();
				}
				
				@Override
				public void onBegin() {
					if(mCurrentTag == AppContances.TAB_ID_SUC_ORDER && mCurrentTag == AppContances.TAB_ID_FAILURE_ORDER){
						return;
					}
					showLoading();
				}

				@Override
				public void onError(String errorMessage) {
					if(!"".equals(errorMessage) && (mCurrentTag != AppContances.TAB_ID_FAILURE_ORDER && mCurrentTag != AppContances.TAB_ID_SUC_ORDER)){
						m_errorTextView.setText(errorMessage);
						m_errorTextView.setVisibility(View.VISIBLE);
			        }else{
			        	m_errorTextView.setVisibility(View.GONE);
			        }
				}
				
				@Override
				public void onShowView() {
					mProgressLayout.setVisibility(View.GONE);
				}
			});
        	FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mModifyOrderFragment, mCurrentTag);
        }
        showFragment(mModifyOrderFragment);
	}
	
	/**
	 * 展示下单失败界面
	 */

	@Override
	public void showOrderFailFragment(final DataItemDetail mDetail){
		super.showOrderFailFragment(mDetail);
		mCurrentTag = AppContances.TAB_ID_FAILURE_ORDER;
		mFailFragment = (OrderFailFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
        if(null == mFailFragment){
        	mFailFragment = new OrderFailFragment();
        	Bundle bundle = new Bundle();
        	bundle.putParcelable("mDetail", mDetail);
        	mFailFragment.setArguments(bundle);
        	mFailFragment.setBtnClickListener(new BtnClickListener() {

				@Override
				public void onBtnClick(int viewID) {
					showOrderFragment();
				}
			});
        	FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mFailFragment, mCurrentTag);
        }else{
        	mFailFragment.setData(mDetail);
        }
        m_errorTextView.setVisibility(View.GONE);
        showFragment(mFailFragment);
	}
	
	/**
	 * 展示下单成功界面
	 */

	@Override
	public void showOrderSucFragment(DataItemDetail mDetail){
		super.showOrderSucFragment(mDetail);
		mCurrentTag = AppContances.TAB_ID_SUC_ORDER;
		mSucFragment = (OrderSucFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
        if(null == mSucFragment){
        	mSucFragment = new OrderSucFragment();
        	Bundle bundle = new Bundle();
        	bundle.putParcelable("mDetail", mDetail);
        	mSucFragment.setArguments(bundle);
        	FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mSucFragment, mCurrentTag);
        }else{
        	mSucFragment.setSucData(mDetail);
        }
        m_errorTextView.setVisibility(View.GONE);
        showFragment(mSucFragment);
	}

	@Override
	public void onSymbolNotify(DataItemDetail tickModel) {
		super.onSymbolNotify(tickModel);
		if(null != mModifyOrderFragment){
			mModifyOrderFragment.onSymbolNotify(tickModel);
		}
	}

	@Override
	public void onSymbolUpdateNotify() {
		if(null != mModifyOrderFragment){
			mModifyOrderFragment.onSymbolNotify();
		}
	}
}
